﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：水表信息维护
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.YongHuGL
{
    public partial class ShuiBiaoXXWH : BasePage
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int ShuiBiaoXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_ShuiBiaoXXID"]);
            }
            set
            {
                ViewState["_ShuiBiaoXXID"] = value;
            }
        }

        /// <summary>
        /// 记录当前查询的水表号,保存在dictionary中,方便实现查询内容记忆功能
        /// </summary>
        private Dictionary<string, string> shuiBiaoBHs
        {
            get
            {
                if (ViewState["shuiBiaoBHs"] == null)
                {
                    ViewState["shuiBiaoBHs"] = new Dictionary<string, string>();

                }
                return ViewState["shuiBiaoBHs"] as Dictionary<string, string>;
            }
        }

        /// <summary>
        /// 当前操作用户站点
        /// </summary>
        private string ZHANDIANBH
        {
            get
            {
                return Convert.ToString(ViewState["_ZHANDIANBH"]);
            }
            set
            {
                ViewState["_ZHANDIANBH"] = value;
            }
        }

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 水表信息对象
        /// </summary>
        YS_CB_SHUIBIAOXX _YS_CB_SHUIBIAOXX = new YS_CB_SHUIBIAOXX();

        /// <summary>
        /// 水表信息BLL
        /// </summary>
        YS_CB_SHUIBIAOXXBLL _YS_CB_SHUIBIAOXXBLL = new YS_CB_SHUIBIAOXXBLL();

        /// <summary>
        /// 用户水表关系BLL
        /// </summary>
        YS_CB_YONGHUSBGXBLL _YS_CB_YONGHUSBGXBLL = new YS_CB_YONGHUSBGXBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
            }
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        /// <summary>
        /// 左侧列表焦点变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_FocusedRowChanged(object sender, EventArgs e)
        {
            if (this.gvList.FocusedRowIndex == -1)
            {
                return;
            }
            //记录当前选中项ID
            object id = this.gvList.GetRowValues(this.gvList.FocusedRowIndex, "ID");
            if (id != null)
            {
                this.ShuiBiaoXXID = Convert.ToInt32(id);
                SetData();
            }
            else
            {
                this.ShuiBiaoXXID = -1;
            }
        }

        /// <summary>
        /// 修改操作
        /// </summary>
        protected void lnbModify_Command(object sender, CommandEventArgs e)
        {
            this.ShuiBiaoXXID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Update;
            Display();
            SetData();;
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            this.ShuiBiaoXXID = Convert.ToInt32(e.CommandArgument);
            _YS_CB_SHUIBIAOXX = this._YS_CB_SHUIBIAOXXBLL.SelectYS_CB_SHUIBIAOXX(this.ShuiBiaoXXID);
            //清除该水表对应的编号记录
            this.shuiBiaoBHs.Remove(this._YS_CB_SHUIBIAOXX.S_SHUIBIAOBH);
            //清除结果集中对应记录
            for (int i = 0; i < this.dtSource.Rows.Count;i++ )
            {
                if (this.dtSource.Rows[i]["S_SHUIBIAOBH"].ToString() == this._YS_CB_SHUIBIAOXX.S_SHUIBIAOBH)
                {
                    this.dtSource.Rows.RemoveAt(i);
                }
            }
            //清空右侧内容区域,重新绑定
            this.InitData();
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            if (this.dtSource.Rows.Count > 0)
            {
                this.gvList.FocusedRowIndex = -1;
                this.gvList.FocusedRowIndex = 0;
            }
        }

        /// <summary>
        /// 添加操作
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        ///放弃操作
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            Display();
        }

        /// <summary>
        /// 清空列表中记录
        /// </summary>
        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.shuiBiaoBHs.Clear();
            this.dtSource = null;
            this.gvList.DataSource = null;
            this.gvList.DataBind();
            InitData();           
        }

        /// <summary>
        /// 清除当前选中项外的其他记录
        /// </summary>
        protected void btnHoldSelected_Click(object sender, EventArgs e)
        {
            //取出未选中的行
            List<DataRow> rows = new List<DataRow>(); ;
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                if (Convert.ToInt32(this.dtSource.Rows[i]["ID"]) != this.ShuiBiaoXXID)
                {
                    rows.Add(this.dtSource.Rows[i]);
                }
            }
            //移除未选中的行
            for (int i = 0; i < rows.Count; i++)
            {
                this.shuiBiaoBHs.Remove(rows[i]["S_SHUIBIAOBH"].ToString());
                this.dtSource.Rows.Remove(rows[i]);                
            }
            //绑定
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            if (this.dtSource.Rows.Count > 0)
            {
                this.gvList.FocusedRowIndex = -1;
                this.gvList.FocusedRowIndex = 0;
            }
        }

        /// <summary>
        /// 查询操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Search;
            LoadData();
            Display();
        }

        /// <summary>
        /// 保存操作
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                SaveData();
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //加载考核复用
            WordsManager manger = new WordsManager();
            PageTools.BindComboBox(this.cmbIType, manger.Instance.FindWordsDataTableByBelongID(WordsStruct.BelongID.水表类型));
            if (this.cmbIType.Items.Count != 0)
            {
                this.cmbIType.SelectedIndex = 0;
            }
            //加载行政区
            PageTools.BindComboBox(
                this.cmbXingZhengQu, BusinessTool.GetTextAndValue(
                    "YS_JX_XINGZHENGQ", "S_MINGCHENG", "S_BIANHAO", null, null
                ).Tables[0]
            );
            this.cmbXingZhengQu.SelectedIndex = 0;
            //获取当前操作人所属站点
            this.ZHANDIANBH = BusinessTool.ZHANDIANBH();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtBiaoCeH.Text = "";
            this.txtBiaoXing.Text = "";
            this.txtKouJing.Text = "";
            this.txtShuiBiaoBH.Text = "";
            this.txtShuiBiaoDZ.Text = "";
            this.txtShuiBiaoHM.Text = "";
            this.txtYongHuH.Text = "";
            this.txtYongHuM.Text = "";
            this.txtZhuangBiaoRQ.Text = "";
            this.cmbIType.Value = "";
            this.cmbXingZhengQu.Value = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            //计算要查询的水表ID
            string bhs = "";
            foreach (var item in this.shuiBiaoBHs.Keys)
            {
                bhs = bhs + item + ",";
            }

            if (this.txtConShuiBiaoH.Text != "")
            {
                //判断是否存在,不存在弹提示
                DataRowCollection drc = _spHelper.P_YS_CB_SHUIBIAOYONGHUNOW(string.Format("S_SHUIBIAOBH='{0}' {1}", this.txtConShuiBiaoH.Text, this.ZHANDIANBH == "00" ? "" : " and S_ZHANDIANBH='" + this.ZHANDIANBH + "'"), null).Tables[0].Rows;
                if(drc.Count==0)
                {
                    PageTools.AlertMsg(this,"没有该水表信息!请重新查询");
                }
                //判断查询的水表号是否已经存在记忆列表中,不存在则添加
                if (!this.shuiBiaoBHs.ContainsKey(this.txtConShuiBiaoH.Text))
                {
                    this.shuiBiaoBHs.Add(this.txtConShuiBiaoH.Text, this.txtConShuiBiaoH.Text);
                    bhs = bhs + this.txtConShuiBiaoH.Text + ",";
                }
            }
            if (bhs == "")
            {
                return;
            }
            else
            {
                bhs = "," + bhs;
            }
            //查询
            DataTable dt = _spHelper.P_YS_CB_SHUIBIAOYONGHUNOW(string.Format("instr('{0}',','||S_SHUIBIAOBH||',',1,1)>0 {1}", bhs, this.ZHANDIANBH == "00" ? "" : " and S_ZHANDIANBH='" + this.ZHANDIANBH + "'"), string.Format("instr('{0}',','||S_SHUIBIAOBH||',',1,1)", bhs)).Tables[0];
            this.dtSource = dt;
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            //定位刚查询的数据
            if (this.dtSource.Rows.Count > 0)
            {
                PageTools.FocusOpreateRecord(this.dtSource, this.gvList, "S_SHUIBIAOBH", this.txtConShuiBiaoH.Text, true);
            }
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    //启用gridview焦点行
                    this.gvList.Enabled = true;
                    this.gvList.Columns[2].SetColVisible(true);
                    this.gvList.Columns[3].SetColVisible(true);
                    SetText(false);
                    this.panListBtns.Enabled = true;
                    this.btnSave.Visible = false;
                    this.btnCancel.Visible = false;
                    break;
                case GridViewStatus.Search:
                    //启用gridview焦点行
                    this.gvList.Enabled = true;
                    this.gvList.Columns[2].SetColVisible(true);
                    this.gvList.Columns[3].SetColVisible(true);
                    SetText(false);
                    this.panListBtns.Enabled = true;
                    this.btnSave.Visible = false;
                    this.btnCancel.Visible = false;
                    break;
                case GridViewStatus.Update:
                    //禁用gridview焦点行
                    this.gvList.Enabled = false;
                    this.gvList.Columns[2].SetColVisible(false);
                    this.gvList.Columns[3].SetColVisible(false);
                    SetText(true);
                    this.panListBtns.Enabled = false;
                    this.btnSave.Visible = true;
                    this.btnCancel.Visible = true;
                    this.cmbIType.Focus();
                    break;
                case GridViewStatus.Add:
                    break;
            }
        }

        /// <summary>
        /// 设置输入框状态
        /// </summary>
        private void SetText(bool b)
        {
            foreach (var item in this.panEdit.Controls)
            {
                DevExpress.Web.ASPxWebControl control = item as DevExpress.Web.ASPxWebControl;
                if (control != null)
                {
                    control.Enabled = b;
                }
            }
            this.txtShuiBiaoBH.Enabled = false;
            this.txtYongHuH.Enabled = false;
            this.txtYongHuM.Enabled = false;
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            //获取当前操作用户信息
            this._YS_CB_SHUIBIAOXX = this._YS_CB_SHUIBIAOXXBLL.SelectYS_CB_SHUIBIAOXX(this.ShuiBiaoXXID);
            this.txtBiaoCeH.Text = this._YS_CB_SHUIBIAOXX.S_CH;
            this.txtBiaoXing.Text = this._YS_CB_SHUIBIAOXX.S_BIAOXING;
            this.txtKouJing.Text = this._YS_CB_SHUIBIAOXX.I_KOUJING.ToString();
            this.txtShuiBiaoBH.Text = this._YS_CB_SHUIBIAOXX.S_SHUIBIAOBH;
            this.txtShuiBiaoDZ.Text = this._YS_CB_SHUIBIAOXX.S_DIZHI;
            this.txtShuiBiaoHM.Text = this._YS_CB_SHUIBIAOXX.S_MINGCHENG;
            this.txtZhuangBiaoRQ.Value = this._YS_CB_SHUIBIAOXX.D_ZHUANGBIAO;
            this.cmbIType.Value = this._YS_CB_SHUIBIAOXX.I_TYPE.ToString();
            this.cmbXingZhengQu.Value = this._YS_CB_SHUIBIAOXX.S_GUANLIKBH;
            //获取对应用户信息
            YS_CB_YONGHUSBGXBLL yhsbBLL = new YS_CB_YONGHUSBGXBLL();
            DataTable dtYhsb = yhsbBLL.SelectYS_CB_YONGHUSBGXesDynamic(string.Format("I_JLZT=0 and I_LEIXING=1 and S_SHUIBIAOBH='{0}'", this.txtShuiBiaoBH.Text), null);
            //如果用户水表关系不是正常状态，则不加载用户信息
            if (Convert.ToString(dtYhsb.Rows[0]["I_SHUIBIAOZT"]) != "0")
            {
                this.txtYongHuH.Text = "";
                this.txtYongHuM.Text = "";
            }
            else
            {
                YS_YG_YONGHUXXBLL bllYH = new YS_YG_YONGHUXXBLL();
                DataTable dtYH = bllYH.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 and S_YONGHUH='{0}' and not exists(select 1 from YS_YG_YONGHUXX S where s.S_YONGHUH=YS_YG_YONGHUXX.S_YONGHUH AND s.I_LEIXING>YS_YG_YONGHUXX.I_LEIXING)", Convert.ToString(dtYhsb.Rows[0]["S_YONGHUBH"])), null);
                if (dtYH.Rows.Count == 1)
                {
                    this.txtYongHuH.Text = dtYH.Rows[0]["S_YONGHUH"].ToString();
                    this.txtYongHuM.Text = dtYH.Rows[0]["S_YONGHUM"].ToString();
                }
            }            
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //当前无操作的数据则返回
            if (this.Status == GridViewStatus.Init)
            {
                return;
            }
            //修改操作
            operateState = OperateState.Modify;
            this._YS_CB_SHUIBIAOXX = this._YS_CB_SHUIBIAOXXBLL.SelectYS_CB_SHUIBIAOXX(this.ShuiBiaoXXID);
            this._YS_CB_SHUIBIAOXX.S_CH = this.txtBiaoCeH.Text;
            this._YS_CB_SHUIBIAOXX.S_BIAOXING = this.txtBiaoXing.Text;
            this._YS_CB_SHUIBIAOXX.I_KOUJING = Convert.ToDecimal(this.txtKouJing.Text);
            this._YS_CB_SHUIBIAOXX.S_SHUIBIAOBH = this.txtShuiBiaoBH.Text;
            this._YS_CB_SHUIBIAOXX.S_DIZHI = this.txtShuiBiaoDZ.Text;
            this._YS_CB_SHUIBIAOXX.S_MINGCHENG = this.txtShuiBiaoHM.Text;
            if (this.txtZhuangBiaoRQ.Value != null)
            {
                this._YS_CB_SHUIBIAOXX.D_ZHUANGBIAO = Convert.ToDateTime(this.txtZhuangBiaoRQ.Value);
            }
            else
            {
                this._YS_CB_SHUIBIAOXX.D_ZHUANGBIAO = null;
            }
            this._YS_CB_SHUIBIAOXX.I_TYPE = Convert.ToDecimal(this.cmbIType.Value);
            this._YS_CB_SHUIBIAOXX.S_GUANLIKBH = this.cmbXingZhengQu.Value.ToString();
            this._YS_CB_SHUIBIAOXX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_CB_SHUIBIAOXX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            //记忆一下水表号,保存后方便定位数据
            string shuibaoH = this.txtShuiBiaoBH.Text;
            //其他信息
            _SYS_LOGBLL.Log<YS_CB_SHUIBIAOXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "水表信息维护", operateState, _YS_CB_SHUIBIAOXX, null);
            if (_YS_CB_SHUIBIAOXXBLL.InsertOrUpdate(_YS_CB_SHUIBIAOXX, null))
            {
                
                 PageTools.AlertMsg(this,"保存成功！");
                this.Status = GridViewStatus.Init;
                Display();
                LoadData();
                this.ShuiBiaoXXID = PageTools.FocusOpreateRecord(this.dtSource, this.gvList, "S_SHUIBIAOBH", shuibaoH, true);             
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
        }

        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (this.txtKouJing.Text == "")
            {
                PageTools.AlertMsg(this,"请输入口径！");
                this.txtKouJing.Focus();
                return false;
            }
            if (!PageTools.ValidateString(this.txtKouJing.Text,PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this,"请输入正确数值！");
                this.txtKouJing.Focus();
                return false;
            }
            return true;
        }

        #endregion

    }
}